#include "str.h"
#include <math.h>
#include <string.h>
int str2float(char* in,double *value_d,int *point_num)
{
	double 	value=0;
	char 	*ptr=in;
	int 	sign=1,point=0;
	int 	len=0;
	if(*ptr=='-')
	{
		ptr++;
		sign=-1;
		len++;
	}
	if (*ptr == '+')
	{
		ptr++;
		sign = 1;
		len++;
	}
	while(*ptr>='0'&&*ptr<='9')
	{
		value=value*10+*ptr++-'0';
		len++;
	}
	if(*ptr=='.'&&ptr[1]>='0'&&ptr[1]<='9')
	{
		ptr++;
		len++;
		while(*ptr>='0'&&*ptr<='9')
		{
			value=value*10+*ptr++-'0';
			point--;
			len++;
			(*point_num)++;
		}
	}
	*value_d=sign*value*pow(10,point);
	return len;
}
//11,1.23,10E3,-10e-3,
double str2num(char* in, int* point_num)
{
	int scale_int=0;
	double value_d=0,value=0;
	char *ptr=in;
	ptr+=str2float(in,&value,point_num);
	if(*ptr=='E'||*ptr=='e')
	{
		ptr++;
		str2float(ptr,&value_d,point_num);
		scale_int=1;
	}
	if(scale_int==1)
		value=value*pow(10,value_d);
	return value;
}

int int2str(int value, char* out)
{
	int i=1,len=0;
	char *ptr=out;
	while(value/i)
		i*=10;
	if(value<0)
	{
		*ptr++='-';
		len++;
	}
	if(value==0)
		i=10;
	while(i/=10)
	{
		*ptr++=value/i+'0';
		value%=i;
		len++;
	}
	*ptr='\0';
	return len; 
}
#define MAX_DIGITS     15
int float2str(double value,int point_num,char* out)
{
	char *ptr=out;
	double value_d = 0, dRound = 0;
	int len=0,i=1;
	len=int2str((int)value,ptr);
	ptr+=len;
	value_d=value-(int)value;
	if(value_d!=0)
	{
		*ptr++='.';
		dRound = 5;
		for (int i = 0; i < MAX_DIGITS; i++) dRound *= 0.1;
		len++;
	}
	while(point_num--)
	{
		value_d= (value_d+ dRound)*10;
		ptr+=int2str((int)value_d,ptr);
		value_d=value_d-(int)value_d;		
		len++;
	}
	*ptr='\0';
	len++;
	return len; 
}

